<?php
/**
 * Created by allen.qiang.
 * 会员部圣诞节活动ajax
 * Date: 14-12-08
 * @author allen.qiang <allen.qiang@fistforward.com>
 * FUN-AC-0026
 */
quanli_require('module/func/eai.func.php');
quanli_require('module/base/activity/activitySpike.class.php');
quanli_require('module/base/activity/activityConfig.class.php');//活动配置文件
quanli_include('module/base/promotion/quanliCoupon.class.php');//加载优惠券类

class ajax_xmas{

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1://用户抽奖信息
                    $this->check();
                    break;
                case 2://用户领奖(优惠券)
                    $this->selectedCode();
                    break;
                case 3://用户领奖（免费蛋糕）
                    $this->selectedCake();
                    break;
                //add by 20141215 allen.qiang FUN-AC-0029-姜饼项目 start
                case 4: //添加商品到购物车
                    $this->nowBuy();
                    break;
                //add by 20141215 allen.qiang FUN-AC-0029-姜饼项目 end
            }

        } else {

            $this->toReturn('参数错误');

        }

    }

    function check(){
        global $G_SHOP;
        if(!$G_SHOP->currentPCustID){
            returnJsonData("未登录", 400);
        }
        $uid=make_uuid();//生成uuid
        $date=date('Y-m-d');
        $starttime=$date." 00:00:00";
        $endtime=$date." 23:59:59";
        $sql="call Luck_draw(@a,'".$G_SHOP->currentPCustID."','".$uid."')";
        $G_SHOP->DBCA->stmt_query($sql);
        $sql="select @a as num";
        $res=$G_SHOP->DBCA->stmt_query($sql);//获取抽中的数据信息
        //用户当天抽奖的次数
        $sql_pcu="select count(1) as sum from activities_extend where activityNumber=? and extend_8=? and createTime > ? and createTime < ?";
        $sumquery=$G_SHOP->DBCA->stmt_query($sql_pcu,'ssss',44000,$G_SHOP->currentPCustID,$starttime,$endtime);
        $query=array(
            'num'=>$res[0]->num,
            'sum'=>$sumquery[0]->sum
        );
        returnJsonData($query, 200);
    }
    //用户领奖(优惠券)
    function selectedCode(){
        global $G_SHOP;
        //实例优惠券类
        $ebsigCoupon = new quanliCoupon();
        $activity_config = new activityConfig('xmas'); //活动配置
        $config_array = $activity_config->config;
        $zr=explode(",",$config_array['couponID']);//感恩节数组$zr[0]10元 $zr[1]20元 $zr[2]30元 $zr[3]50元 $zr[4]100元
        $number=$_REQUEST['number'];
        $date=date('Y-m-d');
        $starttime=$date." 00:00:00";
        $endtime=$date." 23:59:59";
        $activity_sql="update  activities_extend set extend_6=? where extend_8=? and activityNumber=? and  createTime > ? and createTime < ? order by createTime desc  LIMIT 1";
        $activity_queryl=$G_SHOP->DBCA->stmt_query($activity_sql,'sssss',1,$G_SHOP->currentPCustID,44000,$starttime,$endtime);
        //发送优惠券
        if($number<188){

            $cid=$number == 10 ? $zr[0] :( $number == 20 ? $zr[1] :( $number == 30 ? $zr[2] :( $number==50 ? $zr[3] : ( $number==100 ? $zr[4] :0))));
            if($cid == 0 ){
                returnJsonData("优惠券信息不存在", 400);
            }else{
                //会员号
                // $G_SHOP->currentPCustID;
                $cust_sql="select  custID  from  cust_master where  pcustID=?";
                $cust_query=$G_SHOP->DBCA->stmt_query($cust_sql,'s',$G_SHOP->currentPCustID);
                //会员名
                $c_array[] = $cust_query[0]->custID;
                //发送优惠券
                $ebsigCoupon->createCouponCode($cid,$c_array);
            }
        }
        returnJsonData("领取成功", 200);

    }
    //用户领奖（免费蛋糕）
    function selectedCake(){
        global $G_SHOP;
        $date=date('Y-m-d');
        $starttime=$date." 00:00:00";
        $endtime=$date." 23:59:59";
        $activity_sql="update  activities_extend set extend_9=?,extend_10=? where extend_6=? and extend_8=? and activityNumber=? and  createTime > ? and createTime < ? ";
        $activity_queryl=$G_SHOP->DBCA->stmt_query($activity_sql,'sssssss',$_REQUEST['user'],$_REQUEST['mobilePhone'],1,$G_SHOP->currentPCustID,44000,$starttime,$endtime);
        returnJsonData("领取成功", 200);


    }

    //多个商品加入购物车
    public  function nowBuy() {
        quanli_require('module/base/shop/shopCart.class.php');

        if (!isset($_REQUEST['postID'], $_REQUEST['goods_amount'])) {
            returnJsonData('参数错误');
        }
        //实例购物车类
        $shopCart = new shopCart();
        //添加多个商品到购物车
        $result_array = $shopCart->nowBuy_Multiple($_REQUEST['postID'], $_REQUEST['goods_amount'],$_REQUEST['goods_type']);

        returnJsonData($result_array['message'], $result_array['code']);
    }


}